/*
 * Class FileManager
 *
 * Version 1.0
 *
 * Friday, August 22, 2008
 *
 * Created by Palidino76
 */

package palidino76.rs2.io;

import java.io.*;
import palidino76.rs2.players.Player;
import palidino76.rs2.util.Stream;
import palidino76.rs2.util.Misc;

public class FileManager {
    /**
     * Byte buffer for storing bytes to be loaded or saved.
     */
    private Stream stream = new Stream(2000, 2000);

    /**
     * Saves a character file.
     * @param p The player to save.
     */
    public void saveCharacter(Player p) throws Exception {
        if (p == null) {
            return; 
        }
        stream.outOffset = 0;
        stream.writeString("username:" + p.username);
        stream.writeString("password:" + Misc.stringToLong(p.password));
        stream.writeString("rights:" + p.rights);
        stream.writeString("absx:" + p.absX);
        stream.writeString("absy:" + p.absY);
        stream.writeString("height:" + p.heightLevel);
        stream.writeString("runenergy:" + p.runEnergy);
        stream.writeString("attackstyle:" + p.attackStyle);
        stream.writeString("autoretaliate:" + p.autoRetaliate);
        stream.writeString("specialamount:" + p.specialAmount);
        stream.writeString("skulltimer:" + p.skulledDelay);
        stream.writeString("gender:" + p.gender);
        for (int i = 0; i < p.look.length; i++) {
            stream.writeString("look" + i + ":" + p.look[i]);
        }
        for (int i = 0; i < p.color.length; i++) {
            if (p.color[i] > 0)
                stream.writeString("color" + i + ":" + p.color[i]);
        }
        for (int i = 0; i < p.skillLvl.length; i++) {
            stream.writeString("skill" + i + ":" + p.skillLvl[i] + "," + p.skillXP[i]);
        }
        for (int i = 0; i < p.equipment.length; i++) {
            if (p.equipment[i] > -1 && p.equipmentN[i] > 0)
                stream.writeString("equipment" + i + ":" + p.equipment[i] + "," + p.equipmentN[i]);
        }
        for (int i = 0; i < p.items.length; i++) {
            if (p.items[i] > -1 && p.itemsN[i] > 0)
                stream.writeString("item" + i + ":" + p.items[i] + "," + p.itemsN[i]);
        }
        for (int i = 0; i < p.bankItems.length; i++) {
            if (p.bankItems[i] > -1 && p.bankItemsN[i] > 0)
                stream.writeString("bankitem" + i + ":" + p.bankItems[i] + "," + p.bankItemsN[i]);
        }
        stream.writeString("null");
        FileOutputStream out = new FileOutputStream("./data/characters/mainsave/" + p.username + ".dat");
        out.write(stream.outBuffer, 0, stream.outOffset);
        out.flush();
        out.close();
        out = null;
    }

    /**
     * Loads a character file.
     * @param p The player to save.
     */
    public void loadCharacter(Player p) {
        stream.inOffset = 0;
        try {
            FileInputStream in = new FileInputStream("./data/characters/mainsave/" + p.username + ".dat");
            in.read(stream.inBuffer);
            in.close();
            in = null;
        } catch (Exception e) {
            return;
        }
        String line;
        try {
            while ((line = stream.readString()) != null && line.length() > 0 && !line.equals("null")) {
                if (line.startsWith("password:"))
                    p.password = Misc.longToString(Long.parseLong(line.substring(9)));
                else if (line.startsWith("rights:"))
                    p.rights = Integer.parseInt(line.substring(7));
                else if (line.startsWith("absx:"))
                    p.teleportToX = Integer.parseInt(line.substring(5));
                else if (line.startsWith("absy:"))
                    p.teleportToY = Integer.parseInt(line.substring(5));
                else if (line.startsWith("height:"))
                    p.heightLevel = Integer.parseInt(line.substring(7));
                else if (line.startsWith("runenergy:"))
                    p.runEnergy = Integer.parseInt(line.substring(10));
                else if (line.startsWith("attackstyle:"))
                    p.attackStyle = Integer.parseInt(line.substring(12));
                else if (line.startsWith("autoretaliate:"))
                    p.autoRetaliate = Integer.parseInt(line.substring(14));
                else if (line.startsWith("specialamount:"))
                    p.specialAmount = Integer.parseInt(line.substring(14));
                else if (line.startsWith("skulltimer:"))
                    p.skulledDelay = Integer.parseInt(line.substring(11));
                else if (line.startsWith("gender:"))
                    p.gender = Integer.parseInt(line.substring(7));
                else if (line.startsWith("look"))
                    p.look[Integer.parseInt(line.substring(4, 5))] = Integer.parseInt(line.substring(6));
                else if (line.startsWith("color"))
                    p.color[Integer.parseInt(line.substring(5, 6))] = Integer.parseInt(line.substring(7));
                else if (line.startsWith("skill")) {
                    p.skillLvl[Integer.parseInt(line.substring(5, line.indexOf(":")))] = Integer.parseInt(line.substring(line.indexOf(":") + 1, 
                        line.indexOf(",")));
                    p.skillXP[Integer.parseInt(line.substring(5, line.indexOf(":")))] = Integer.parseInt(line.substring(line.indexOf(",") + 1));
                } else if (line.startsWith("equipment")) {
                    p.equipment[Integer.parseInt(line.substring(9, line.indexOf(":")))] = Integer.parseInt(line.substring(line.indexOf(":") + 1, 
                        line.indexOf(",")));
                    p.equipmentN[Integer.parseInt(line.substring(9, line.indexOf(":")))] = Integer.parseInt(line.substring(line.indexOf(",") + 1));
                } else if (line.startsWith("item")) {
                    p.items[Integer.parseInt(line.substring(4, line.indexOf(":")))] = Integer.parseInt(line.substring(line.indexOf(":") + 1, 
                        line.indexOf(",")));
                    p.itemsN[Integer.parseInt(line.substring(4, line.indexOf(":")))] = Integer.parseInt(line.substring(line.indexOf(",") + 1));
                } else if (line.startsWith("bankitem")) {
                    p.bankItems[Integer.parseInt(line.substring(8, line.indexOf(":")))] = Integer.parseInt(line.substring(line.indexOf(":") + 1, 
                        line.indexOf(",")));
                    p.bankItemsN[Integer.parseInt(line.substring(8, line.indexOf(":")))] = Integer.parseInt(line.substring(line.indexOf(",") + 1));
                }
            }
        } catch (Exception e) {
        }
    }

    public void appendData(String file, String text) {
        BufferedWriter bw = null;
        try {
            FileWriter fileWriter = new FileWriter("./data/" + file, true);
            bw = new BufferedWriter(fileWriter);
            bw.write(text);
            bw.newLine();
            bw.flush();
            bw.close();
            fileWriter = null;
            bw = null;
        } catch (Exception exception) {
            Misc.println("Critical error while writing data: " + file);
        }
    }
}